
import { _decorator, Component, Game, Node, Sprite } from 'cc';
import EvtManger from '../tools/EvtManger';
import { ConfigKey, EventName } from '../manager/AppControll';
import { GiftType } from '../pop/TemporaryGift';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
const { ccclass, property } = _decorator;

@ccclass('Buff')
export class Buff extends Component {
    @property(Node)
    mTripleDamage: Node = null;
    @property(Node)
    mTripleCoins: Node = null;
    @property(Node)
    mNdCoinProgress: Node = null;
    @property(Node)
    mNdDamageProgress: Node = null;
    private mCointTime = 0;
    private mDmgTime = 0;
    private mTotalCoinTime = 0;
    private mTotalDmg = 0;

    protected onLoad(): void {
        this.RegisteredEvent();
    }

    RegisteredEvent() {
        EvtManger.on(EventName.receiveGift, this, this.ReceiveGift)
    }

    ReceiveGift(giftType: GiftType) {
        switch (giftType) {
            case GiftType.tripleCoins:
                this.mCointTime += Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.buffTime).value);
                this.mTripleCoins.active = true;
                this.mTotalCoinTime = this.mCointTime;
                break;
            case GiftType.tripleDamage:
                this.mDmgTime += Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.buffTime).value);
                this.mTripleDamage.active = true;
                this.mTotalDmg = this.mDmgTime;
                break;
            case GiftType.gold:
                GameData.AddGold(GameData.GetGiftGoldReward(), true);
                EvtManger.do(EventName.dropGiftGold);
                break;
        }
    }

    start() {
        // [3]
    }

    update(deltaTime: number) {
        if (this.mCointTime > 0) {
            this.mCointTime -= deltaTime;
            let pre = this.mCointTime / this.mTotalCoinTime;
            this.mNdCoinProgress.getComponent(Sprite).fillRange = (1 - pre);
            GameData.SetGiftGold(3);
        } else if (this.mCointTime <= 0) {
            this.mTripleCoins.active = false;
            GameData.SetGiftGold(1);
        }
        if (this.mDmgTime > 0) {
            this.mDmgTime -= deltaTime;
            let pre = this.mDmgTime / this.mTotalDmg;
            this.mNdDamageProgress.getComponent(Sprite).fillRange = (1 - pre);
            GameData.SetGiftDmg(3);
        } else if (this.mDmgTime <= 0) {
            this.mTripleDamage.active = false;
            GameData.SetGiftDmg(1);
        }
    }
}
